import Vue from 'vue'
import Router from 'vue-router'

import ListUser from '../views/ListUser'
import CreateUser from '../views/CreateUser'
import EditUser from '../views/EditUser'
import Login from '../components/Login.vue'
import Home from '../components/Home.vue'
import Welcome from '../components/Welcome.vue'
import User from '../components/user/User.vue'
import Rights from '../components/power/Rights.vue'
import Cs from '../components/power/Cs.vue'
import Icons from '../components/icon/Index.vue'
import Cate from '../components/goods/Cate.vue'
import Params from '../components/goods/Params.vue'
import List from '../components/goods/List.vue'
import Add from '../components/goods/Add.vue'
import Edit from '../components/goods/Edit.vue'
import Order from '../components/order/Order.vue'
import Report from '../components/report/Report.vue'
import Coupon from '../components/coupon/Coupon.vue'
import Seckill from '../components/seckill/Seckill.vue'
import Goods from '../components/seckill/Goods.vue'
import Banner from '../components/banner/banner.vue'
Vue.use(Router)

const router = new Router({
  routes: [
    { path: '/', redirect: '/login' },
    {
      path: '/login',
      component: Login
    },
    {
      path: '/home',
      component: Home,
      redirect: '/reports',
      children: [
        { path: '/welcome', component: Welcome },
        { path: '/user', component: User },
        { path: '/rights', component: Rights },
        { path: '/icons', component: Icons },
        { path: '/cs', component: Cs },
        { path: '/list', component: List },
        { path: '/cate', component: Cate },
        { path: '/params', component: Params },
        { path: '/goods/add', component: Add },
        { path: '/goods/edit/:id', component: Edit },
        { path: '/orders', component: Order },
        { path: '/reports', component: Report },
        { path: '/coupon', component: Coupon },
        { path: '/seckill', component: Seckill },
        { path: '/seckill/goods/:id', component: Goods },
        { path: '/banner', component: Banner },




      ]
    },

    {
      path: "/user/index",
      name: "list-user",
      component: ListUser
    },
    {
      path: "/user/create",
      name: "create-user",
      component: CreateUser
    },
    {
      path: "/user/edit/:id",
      name: "edit-user",
      component: EditUser
    },
  ]
})

//挂载路由导航守卫
router.beforeEach((to, from, next) => {
  //to 将要访问的路径
  //from 代表从哪个路径跳转而来
  //next 是一个函数，表示放行
  // next() 放行 next('/login') 强制跳转
  if (to.path == '/login') return next()
  //获取token
  const token = window.sessionStorage.getItem('token');
  if (!token) return next('/login')
  next()
})


export default router
